<?php
class form
{
	var $form;
	var $action;
	var $head;
	var $btn;
	var $holder = array();

	function form($form,$action,$head,$btn)
	{
		global $s;
		$this->form		= $form;	// Namn på formuläret
		$this->action	= $action;	// Vart formuläret ska skickas
		$this->head		= $head;	// Rubrik på formuläret (h1)
		$this->btn		= $btn;		// Text på skicka-knappen
		$this->holder['html']	= '';
		$this->holder['js']	= '';
		$this->holder['html_outer'] = $this->form_html_outer($this->form,$this->action,$this->head,$this->btn);	// form_html_outer($form,$action,$head,$btn)
		$this->holder['element_array'] = array();	// Array som håller alla element i formuläret (input, textbox osv)
		$this->holder['check']['error'] = array();	// Array som håller alla element som innehåller fel vid checkForm
		$this->holder['check']['data']  = array();	// Array som innehåller datan som kontrollerats av checkForm
		

	}
	function set_sql_data($from='',$adress_id='')
	{
		global $s, $database;
		if($from == 'adress_info')
		{
			$user_info = $database->get_user_adress_info($adress_id);
		}else{
			$user_info = $database->get_user_info($s->email);
		}
		#print_r($user_info);
		#print_r($this->holder['element_array']);
		#print_r($this->holder['check']['data']);
		foreach($this->holder['element_array'] as $elm_id => $elm_val)
		{
			$id = $this->form.'_'.$elm_val[2];
			#print $id;
			$this->holder['check']['data'][$id] = $user_info[$elm_val[2]];
		}
		#print_r($this->holder['check']['data']);
	}
	function addElm($elm)
	{
		$this->holder['element_array'][$elm[2]] = $elm;
	}
	
	function addArray($array)
	{
		foreach ($array as $x)
		{
			$this->addElm($x);
		}
	}
	function getData()
	{
		return $this->holder['check']['data'];
	}
	function setError($id,$error)
	{
		$this->holder['element_array'][$id][5] = $error;
		$this->holder['check']['error'][$this->form.'_'.$id] = array(1,$error);
	}
	function checkHasError()
	{
		return($this->holder['check']['error'] != array());
	}
	
	function session_error()
	{
		global $s;
		if( isset($_SESSION[$this->form.'_data']) && isset($_SESSION[$this->form.'_error']) )
		{
			$this->holder['check']['data'] = $_SESSION[$this->form.'_data'];
			$this->holder['check']['error'] = $_SESSION[$this->form.'_error'];
			
			unset($_SESSION[$this->form.'_data']);
			unset($_SESSION[$this->form.'_error']);
		}
	}
	
	
	
	function getSql($do,$into,$session_uid = 0,$time = 0,$where = 'epost',$where_val='')
	{
		global $s;
		$data = $this->getData();

		switch($do)
		{
			case 'INSERT':
				$qKeys =  '';
				$qVals =  '';
				foreach ($data as $dataKey => $dataVal)
				{
					$dataKey = substr($dataKey,strlen($this->form.'_'),strlen($dataKey));
					if($dataKey != 'pass2')
					{
						$dataVal = ($dataKey == 'pass') ? md5($dataVal) : $dataVal;
						$qKeys .= "".$dataKey.",";
						$qVals .= "'".$dataVal."',";
					}
				}
				#$qKeys = substr($qKeys,0,strlen($qKeys)-1);
				#$qVals = substr($qVals,0,strlen($qVals)-1);
				if($time)
				{
					$qKeys .= 'time';
					$qVals .= time();
				}else{
					$qKeys = substr($qKeys,0,strlen($qKeys)-1);
					$qVals = substr($qVals,0,strlen($qVals)-1);
				}
				
				if($s->userid > 1)
				{
					return sprintf('INSERT INTO %s (userid,%s) VALUES (%d,%s)',$into,$qKeys,$s->userid,$qVals);
				}
					return sprintf('INSERT INTO %s (userid,%s) VALUES (NULL,%s)',$into,$qKeys,$qVals);
				break;
			case 'UPDATE':
				$key_val = '';
				foreach ($data as $dataKey => $dataVal)
				{
					$dataKey = substr($dataKey,strlen($this->form.'_'),strlen($dataKey));
					if($dataKey != 'pass2' && $dataKey != 'pass_old')
					{
						$dataVal = ($dataKey == 'pass') ? md5($dataVal) : $dataVal;
						$key_val .= "".$dataKey."="."'".$dataVal."',";
					}
				}
				$key_val = substr($key_val,0,strlen($key_val)-1);
				if($where == 'epost')
				{
					return sprintf('UPDATE %s SET %s WHERE epost = \'%s\'',$into,$key_val,$s->email);
				}else{
					return sprintf('UPDATE %s SET %s WHERE %s = \'%s\'',$into,$key_val,$where,$where_val);
				}
				break;
		}

	}
	
	
	
	function form_input2($form,$namn,$rubrik = '',$val='',$typ="text",$error='') {
		if(!$rubrik){ $rubrik = $namn;}
		$html = '
			  <tr>
				<th scope="row">%s</th>
				<td>
				
					<div class="input_left"></div>
					<input type="%s" class="t_input" id="%s_%s" name="%s_%s" value="%s" />
					<div class="input_right" id="%s_%s_error">%s</div>
				</td>
			  </tr>';
		return sprintf($html,$rubrik,$typ,$form,$namn,$form,$namn,$val,$form,$namn,$error);
	}
	function form_input($form,$namn,$rubrik = '',$val='',$typ="text",$error='<!-- -->') {
		if(!$rubrik){ $rubrik = $namn;}
		$html = '
			  <tr>
				<th scope="row">%s</th>
				<td>
					<div class="input_left"></div>
					<input type="%s" class="t_input" id="%s_%s" name="%s_%s" value="%s" onkeypress="{if (event.keyCode==13)submitform_%s()}" />
					<div class="input_right"></div><div class="input_right_error" id="%s_%s_error">%s</div>
				</td>
				<td>
				</td>
			  </tr>';
		return sprintf($html,$rubrik,$typ,$form,$namn,$form,$namn,$val,$form,$form,$namn,$error);
	}
	function form_input_hidden($form,$namn,$val='') {
		$html = '
					<input type="hidden" id="%s_%s" name="%s_%s" value="%s" />';
		return sprintf($html,$form,$namn,$form,$namn,$val);
	}
	function form_textarea($form,$namn,$rubrik = '',$val='',$error='') {
		if(!$rubrik){ $rubrik = $namn;}
		$html = '
			  <tr>
				<td colspan=2">
					<div class="textarea_left"></div>
					<textarea class="t_textarea" id="%s_%s" name="%s_%s" value="" >%s</textarea>
					<div class="textarea_right"></div><div class="input_right_error" id="%s_%s_error">%s</div>
				</td>
				<td>
				</td>
			  </tr>';
		$html_ = '
			  <tr>
				<th scope="row">%s</th>
				<td>
					<div class="textarea_left"></div>
					<textarea class="t_textarea" id="%s_%s" name="%s_%s" value="%s" ></textarea>
					<div class="textarea_right"></div><div class="input_right_error" id="%s_%s_error">%s</div>
				</td>
				<td>
				</td>
			  </tr>';

		#return sprintf($html,$rubrik,$form,$namn,$form,$namn,$val,$form,$namn,$error);
		return sprintf($html,$form,$namn,$form,$namn,$val,$form,$namn,$error);
	}

	function form_text($form,$text,$rubrik = '') {
		if($rubrik == '')
		{ 
			$html = '
			<tr>
			<td colspan="2" scope="row" class="form_text">%s</td><td scope="row"></td>
			</tr>';
			return sprintf($html,$text);
		}
		$html = '
		<tr>
		<td colspan="2" scope="row"><h3>%s</h3><p>%s</p></td><td scope="row"></td>
		</tr>';
		return sprintf($html,$rubrik,$text);
	}
	
	function form_checkbox ($form,$namn,$rubrik = '',$val='') {
		if(!$rubrik){ $rubrik = $namn;}
		if($val == 'on'){$checked = 'checked="checked"';}else{$checked = '';}
		$html = '
		<tr>
		<td colspan="3" scope="row"><label for="%s_%s">%s</label>
		<input type="checkbox" id="%s_%s" name="%s_%s" %s /></td>
		</tr>';
		return sprintf($html,$form,$namn,$rubrik,$form,$namn,$form,$namn,$checked);
	}
	
	function form_html_outer($form,$action,$head,$btn)
	{
		$html = sprintf('
		<h3>%s</h3>
		<form id="%s" name="%s" method="post" action="%s">
		<table border="0" cellpadding="0" cellspacing="0">
		
		%s
		
		<tr>
		<th scope="row"><!--<input type="submit" name="Submit" id="button" value="Submit" />--></th>
		<td colspan="2"><input name="%s_submit" id="%s_submit" type="hidden" value="1" />
		<a href="#" class="btn_a" onclick="submitform_%s()"><span class="btn_left"></span><span class="btn">%s</span><span class="btn_right"></span></a>
		</td>
		</tr>
		
		</table>
		</form>
		',$head,$form,$form,$action,'%s',$form,$form,$form,$btn);	;
		return $html;
	}
	
	
	function checkForm()
	{
		if(isset($_POST[$this->form."_submit"]))
		{
			require_once($_CONFIG['paths']['lib'].'func.form.php');
			foreach ($this->holder['element_array'] as $x) {
				$id = $this->form.'_'.$x[2];
				switch ($x[1]) {
					case 'is_str':
						( !check_is_str($id,$x[5]) ) ? ($this->holder['check']['error'][$id] = 1) : '';
						$this->holder['check']['data'][$id] = check_data($_POST[$id]);
						break;
					case 'is_int':
						( !check_is_int($id,$x[5]) ) ? ($this->holder['check']['error'][$id] = 1) : '';
						$this->holder['check']['data'][$id] = check_data($_POST[$id],1);
						break;
					case 'is_pnr':
						( !check_pnr($id,$x[5]) ) ? ($this->holder['check']['error'][$id] = 1) : '';
						$this->holder['check']['data'][$id] = check_data($_POST[$id],1);
						break;						
					case 'is_email':
						( !check_is_email($id,$x[5]) ) ? ($this->holder['check']['error'][$id] = 1) : '';
						$this->holder['check']['data'][$id] = check_data($_POST[$id]);
						break;
					case 'is_pass':
						( !check_pass($id,$x[5]) ) ? ($this->holder['check']['error'][$id] = 1) : '';
						$this->holder['check']['data'][$id] = check_data($_POST[$id]);
						$temp = $x;
						break;
					case 'is_pass2':
						( !check_pass2($id,$this->form.'_'.$temp[2],$x[5]) ) ? ($this->holder['check']['error'][$id] = 1) : '';
						$this->holder['check']['data'][$id] = check_data($_POST[$id]);
						break;
					default:
						$_POST[$id] = ($_POST[$id] == 'on')?1:$_POST[$id];
						$this->holder['check']['data'][$id] = check_data($_POST[$id]);
						break;
				}
			}
			
			if($this->holder['check']['error'])
			{
				return false;
			}
			return true;
		}
		return false;

	}
	
	function createForm()
	{
		foreach ($this->holder['element_array'] as $x) {
			$id = $this->form.'_'.$x[2];
			$errorprint = ($this->holder['check']['error'][$id])?$x[5]:''; //om idnamn finns i error skriv ut det annars inget
			if ( ($this->holder['check']['error'][$id]) == 1 )
			{
				$errorprint = $x[5];
			}
			elseif ( ($this->holder['check']['error'][$id][0]) == 1 )
			{
				$errorprint = $this->holder['check']['error'][$id][1];
			}

			switch ($x[0]) {
				case 'input':
					$typ = ($x[6])?$x[6]:"text";
					$this->holder['html'] .= $this->form_input($this->form,$x[2],$x[3],$this->holder['check']['data'][$id],$typ,$errorprint);
					break;
				case 'input_hidden':
					$this->holder['html'] .= $this->form_input_hidden($this->form,$x[2],$x[3]);
					break;
				case 'textarea':
					$typ = ($x[6])?$x[6]:"text";
					$this->holder['html'] .= $this->form_textarea($this->form,$x[2],$x[3],$this->holder['check']['data'][$id],$errorprint);
					break;
				case 'checkbox':
					if($this->holder['check']['data'][$id] == 1)
					{
						$this->holder['check']['data'][$id] = 'on';
					}
					elseif($this->holder['check']['data'][$id] == 0)
					{
						$this->holder['check']['data'][$id] = 'off';
					}
					$check = ($this->holder['check']['data'][$id])?$this->holder['check']['data'][$id]:'on';
					$this->holder['html'] .= $this->form_checkbox($this->form,$x[2],$x[3],$check);
					break;
				case 'text':
					$this->holder['html'] .= $this->form_text($this->form,$x[3],$x[4]);
			}
		}		
	}
	
	function createJS()
	{
		$temp = '';
		foreach ($this->holder['element_array'] as $x) {
			$id = $this->form.'_'.$x[2];
			$error = "\n{\n error['".$id."'] = 1 \n}else{\n error['".$id."'] = 0 \n}";
			switch ($x[1]) {
				case 'is_str':
					$this->holder['js'] .= "if( !check_is_str('".$id."','".$x[5]."') ) ".$error."  \n";
					break;
				case 'is_int':
					$this->holder['js'] .= "if( !check_is_int('".$id."','".$x[5]."') ) ".$error."  \n";
					break;
				case 'is_pnr':
					$this->holder['js'] .= "if( !check_pnr('".$id."','".$x[5]."') ) ".$error."  \n";
					break;
				case 'is_email':
					$this->holder['js'] .= "if( !check_is_email('".$id."','".$x[5]."') ) ".$error."  \n";
					break;
				case 'is_pass':
					$this->holder['js'] .= "if( !check_pass('".$id."','".$x[5]."') ) ".$error."  \n";
					$temp = $x;
					break;
				case 'is_pass2':
					$this->holder['js'] .= "if( !check_pass2('".$id."','".$this->form."_".$temp[2]."','".$x[5]."') ) ".$error."  \n";
					break;
			}
		}
	}
	
	function echoJS()
	{
		$alljs = "<script type=\"text/javascript\">
		function submitform_%s()
		{
			var error = new Array();
			var error_check = 0;
			var i = 0;
			%s
			for (i in error)
			{
				if(error[i]) {error_check = 1;}
			}
			if(!error_check)
			{
				document.%s.submit();
			}
		  
		}
		</script>";
		return sprintf($alljs,$this->form,$this->holder['js'],$this->form);
	}
	
	function echoForm()
	{
		return sprintf($this->holder['html_outer'],$this->holder['html']);
	}

}
?>